/**
 * Created by smp on 2017/5/17.
 */
var modelPath=ROOT_PATH+'/archives/personArchives/securityOrganizationLedger/';
var ORGANIZATION_LEDGER_LEVEL=CONSTANT.DictionaryTypeDefConstants.ORGANIZATION_LEDGER_LEVEL;
var ORGANIZATION_LEDGER_CATEGORY=CONSTANT.DictionaryTypeDefConstants.ORGANIZATION_LEDGER_CATEGORY;
$('#category').combobox({
    url:ROOT_PATH+'/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code='+ORGANIZATION_LEDGER_CATEGORY,
    required:true,
    editable:false,
    valueField:'id',
    textField:'text'
});
$('#level').combobox({
    url:ROOT_PATH+'/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code='+ORGANIZATION_LEDGER_LEVEL,
    required:true,
    editable:false,
    valueField:'id',
    textField:'text'
});
$('#unit').combotree({
    url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
});
$("#btn_save").click(function(){
    var index=layer.confirm('是否确定保存？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        var data = $('#precautionList').datagrid('getData');
        var json=JSON.stringify(data.rows);
        $('#form1').form('submit', {
            url: modelPath+'add.shtml',

            onSubmit: function (param) {
                param.organizationRelation=json;
                var isValid = $(this).form('validate');
                if (!isValid) {
                    layer.close(index);
                    layer.msg("请正确填写表单")
                }
                return $(this).form('validate');
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success){
                    layer.msg(result.message,{time:1000,icon:1},function () {
                        WindowManager.closeLayer({
                            returnValue: result
                        });
                    });
                }else {
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });

});

/*==============================================================================*/
 $("#precautionList").datagrid({
    //url: ROOT_PATH+'/hiddenDanger/hdRegister/queryPrecautionList.shtml', //请求的数据源
    iconCls: 'icon-save', //图标
    //pagination: true, //显示分页
    //pageSize: 3, //页大小
    //pageList: [3, 6, 9], //页大小下拉选项此项各value是pageSize的倍数
    rownumbers:true,
    singleSelect:true,
    fit: true, //datagrid自适应宽度
    fitColumns: true, //列自适应宽度
    striped: true, //行背景交换
    nowap: true, //列内容多时自动折至第二行
    //border: false,
    toolbar:'#tb',
    columns: [[//显示的列
        {field:'relationId',width:100,checkbox:true},
        //{field:'dutyUserId',width:100,hidden:true},
        {field:'name',title:'姓名',width:200
        },
        {field:'unit',title:'单位',width:200,hidden:true
        },
        {field:'unitName',title:'单位',width:200
        },
        {field:'category',title:'所属组织台账类别',width:300,hidden:true},
        {field:'categoryName',title:'所属组织台账类别',width:300},
        {field:'level',title:'所属组织台账级别',width:300,hidden:true},
        {field:'levelName',title:'所属组织台账级别',width:300},
        {field:'sex',title:'性别',width:200,hidden:true},
        {field:'sexName',title:'性别',width:200},
        {field:'post',title:'职务/岗位',width:200},
        {field:'place',title:'职称',width:200},
        {field:'degreeEducation',title:'文化程度',width:200,hidden:true},
        {field:'degreeEducationName',title:'文化程度',width:200},
        {field:'birthDate',title:'出生年月',width:300},
        {field:'beginTime',title:'起始日期',width:300},
        {field:'endTime',title:'结束日期',width:300,hidden:true},
        {field:'remark',title:'备注',width:300,hidden:true}
    ]]
});
 $("#btn_precaution_add").click(function(){
     WindowManager.openLayer({
         type: 2,
         title: '新增',
         area: ['70%', '80%'],
         content:modelPath+"information.shtml",
         callBack:function(result){
             $('#precautionList').datagrid('insertRow',{
                 index: 0,  // 索引从0开始
                 row: {
                     id:result.id,
                     name: result.name,
                     unit: result.unit,
                     unitName: result.unitName,
                     category: result.category,
                     categoryName: result.categoryName,
                     level: result.level,
                     levelName: result.levelName,
                     sex: result.sex,
                     sexName:result.sexName,
                     post: result.post,
                     place: result.place,
                     degreeEducation: result.degreeEducation,
                     degreeEducationName: result.degreeEducationName,
                     birthDate: result.birthDate,
                     beginTime: result.beginTime,
                     endTime:result.endTime,
                     remark:result.remark
                 }
             });
         }
     });
 });
 $("#btn_precaution_delete").click(function(){
     var checkedRows=$('#precautionList').datagrid('getChecked');

     if(checkedRows.length == 0){
         layer.alert("选择需要删除的行！");
         return
     }else if (checkedRows.length > 1){
         layer.alert("只能选中一行数据！");
         return
     }
     var rowIndex = $('#precautionList').datagrid('getRowIndex', checkedRows[0]);
     $('#precautionList').datagrid('deleteRow', rowIndex);
     layer.msg("删除成功",{time:1000,icon:1});
 });